﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IntelligentCMS
{
    public partial class CMSRadioButtonMultilineItem : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

       
        private List<string> columns = new List<string>();

        private bool alternate = false;

        public bool Alternate
        {
            set
            {
                alternate = value;
            }

        }
        private string rowheader = string.Empty;

        public void DataBind2()
        {
            string rowbegin = "<tr class=\"itemtemplate\"><td align=\"left\">";

            if (alternate)
            {

                rowbegin = "<tr class=\"alternatetemplate\"><td align=\"left\">";
            }
            
            //private string rowinsert = ;
            string rowend = "</td></tr>";




            Literal_RowBegin.Text = rowbegin + rowheader;
            
            Literal_RowEnd.Text = rowend;



            for (int i = 1; i < columns.Count; i++ )
            {


                Label radiobegin = new Label();
                radiobegin.Text = "</td><td align=\"center\">";

                RadioButton newButton = new RadioButton();
                newButton.GroupName = rowheader;
                newButton.Text = "";

                thePanel.Controls.Add(radiobegin);
                thePanel.Controls.Add(newButton);
                buttons.Add(newButton);

                //theRadioButtonList.Items.Add("");

            }

           
            

        }

        private int getselectedindex()
        {
            int i = 0;
            foreach (RadioButton button in buttons)
            {
                if (button.Checked)
                {

                    return i;
                }
                i++;
            }
            return -1;
        }

        private string generatesqlstring()
        {
            string sqlstring = string.Empty;

            sqlstring += "INSERT INTO [" + strings.databaseprefix + "].[dbo].[" + strings.databaseprefix + "_RadioButtonMultiline] ([ParentName], [Name], [Value], [URL], [Time]) VALUES ('" + common.GetBase64String(parentname) +  "','" + common.GetBase64String(rowheader) + "','" + common.GetBase64String(getselectedindex().ToString()) + "','" + common.GetBase64String(clienturl) + "', GETDATE());";

            return sqlstring;

        }

        private string clienturl = string.Empty;

        public string ClientURL
        {
            set
            {
                clienturl = value;
            }


        }

        private string parentname = string.Empty;

        public string ParentName 
        {

            set 
            {
                parentname = value;

            }


        }

        private List<RadioButton> buttons = new List<RadioButton>();

        private List<string> errorlist()
        {
            List<string> errors = new List<string>();
            foreach (RadioButton b in buttons)
            {
                if (b.Checked || !checkforerrors)
                {
                    
                    return errors;
                }

            }
            errors.Add(rowheader + strings.fielderror);
            return errors;


        }



        public string SQLString
        {
            get
            {
                return generatesqlstring();


            }

        }


        public bool CheckForErrors
        {
            set { checkforerrors = value; }
        }

        private bool checkforerrors = false;

        public List<string> Errors
        {
            get { return errorlist(); }
        }


        public string RowHeader
        {

            set { rowheader = value; }
        }

        public List<string> Columns
        {
            set
            {
                columns = value;
            }

        }
    }
}